<template>
  <div class="gallery-list" @mouseenter="isHover = true" @mouseleave="isHover = false">
    <div class="container-content" :style="{backgroundColor: modelValue?.bgColor}">
      <div class="default-row row" :class="[`row-cols-${modelValue?.mobileCol}`, `row-cols-md-${modelValue?.desktopCol}`]">
        <div v-for="item in modelValue?.data" :key="item.id" class="col">
          <div class="gallery-default">
            <div class="img-wrapper need-mask">
              <img :src="item.imgSrc" :alt="item.alt" />
              <div class="img-wrapper__content">
                <div class="img-wrapper__content__title">
                  {{ item.title }}
                </div> 
                <h5 class="img-wrapper__content__subtitle">
                  {{ item.subtitle }}
                </h5>
              </div>
            </div>
          </div> 
        </div>
      </div>
    </div>

     <Operate v-if="!isPreview" v-model:show="isHover" @handle-edit="showSetting = true" @handle-delete="handleDelete" @handle-copy="handleCopy" @handle-paste="handlePaste"></Operate>
  </div>

  <Setting v-if="!isPreview" v-model:show="showSetting" :data="modelValue"></Setting>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { randomString } from '@/utils/common'
import Setting from './setting.vue'
import Operate from '@/common/Operate/index.vue'

// 定义组件名称
defineOptions({
  name: 'GalleryDefault'
})

const props = defineProps({
  modelValue: {
    type: Object,
    default: () => {}
  },

  // 是否预览
  isPreview: {
    type: Boolean,
    default: true
  }
})

const emit = defineEmits(['update:modelValue', 'handleDelete', 'handleCopy', 'handlePaste'])

const handleInit = () => {
  if (props.modelValue && props.modelValue.data) return // 有数据不用初始化
  const data = {
    id: props.modelValue?.id || randomString(),
    type: 'GalleryDefault',
    bgColor: '',
    desktopCol: 3,
    mobileCol: 2,
    data: [
      {
        id: randomString(),
        title: '标题',
        subtitle: '副标题',
        imgSrc: 'https://osswebsite-test.cedim.cn/media-library/ywies-bj/images/home/ywies-tx.jpg',
        alt: '',
      },
      {
        id: randomString(),
        title: '标题',
        subtitle: '副标题',
        imgSrc: 'https://osswebsite-test.cedim.cn/media-library/ywies-bj/images/home/ywies-tx.jpg',
        alt: '',
      },
    ]
  }
  emit('update:modelValue', data)
} 

const isHover = ref<boolean>(false);
const showSetting = ref<boolean>(false);

const handleDelete = () => {
  emit('handleDelete', props.modelValue.id)
}

const handleCopy = () => {
  emit('handleCopy', props.modelValue)
}

const handlePaste = (type: string) => {
  emit('handlePaste', type)
}

handleInit()
</script>

<style lang="scss" scoped>
.gallery-list {
  position: relative;
  width: 100%;
}

/* Simple row/col grid with responsive row-cols helpers used by this component */
.row {
  --bs-gutter-x: 16px;
  --bs-gutter-y: 0;
  display: flex;
  flex-wrap: wrap;
  margin-top: calc(var(--bs-gutter-y)*-1);
  margin-right: calc(var(--bs-gutter-x)*-0.5);
  margin-left: calc(var(--bs-gutter-x)*-0.5)
}

.row>* {
  flex-shrink: 0;
  width: 100%;
  max-width: 100%;
  padding-right: calc(var(--bs-gutter-x)*0.5);
  padding-left: calc(var(--bs-gutter-x)*0.5);
  margin-top: var(--bs-gutter-y)
}

.col {
  flex: 1 0 0%
}

.row-cols-auto>* {
  flex: 0 0 auto;
  width: auto
}

.row-cols-1>* {
  flex: 0 0 auto;
  width: 100%
}

.row-cols-2>* {
  flex: 0 0 auto;
  width: 50%
}

.row-cols-3>* {
  flex: 0 0 auto;
  width: 33.3333333333%
}

.row-cols-4>* {
  flex: 0 0 auto;
  width: 25%
}

.row-cols-5>* {
  flex: 0 0 auto;
  width: 20%
}

.row-cols-6>* {
  flex: 0 0 auto;
  width: 16.6666666667%
}

@media(min-width: 768px) {
  .row-cols-md-auto>* {
      flex: 0 0 auto;
      width: auto
  }

  .row-cols-md-1>* {
      flex: 0 0 auto;
      width: 100%
  }

  .row-cols-md-2>* {
      flex: 0 0 auto;
      width: 50%
  }

  .row-cols-md-3>* {
      flex: 0 0 auto;
      width: 33.3333333333%
  }

  .row-cols-md-4>* {
      flex: 0 0 auto;
      width: 25%
  }

  .row-cols-md-5>* {
      flex: 0 0 auto;
      width: 20%
  }

  .row-cols-md-6>* {
      flex: 0 0 auto;
      width: 16.6666666667%
  }
}

/* image wrapper helpers */
.img-wrapper {
  position: relative;
  height: 0;
  width: 100%
}
.img-wrapper img {
  position: absolute;
  width: 100%;
  height: 100%;
  -o-object-fit: cover;
  object-fit: cover
}

.default-row {
  --bs-gutter-x: 12px;
  --bs-gutter-y: 12.79px;

  @media(min-width: 768px) {
    --bs-gutter-x: 8px;
    --bs-gutter-y: 8.37px;
  }
}
.wide-row {
  --bs-gutter-y: 12.79px;

  @media(min-width: 768px) {
    --bs-gutter-y: 8.37px;
  }
}
.caption-row { 
  --bs-gutter-x: 16px;
  --bs-gutter-y: 24px;
}

.gallery-default {
  width: 100%;
  height: 100%;
}

.gallery-default .img-wrapper {
  padding-bottom: 61.833%;
}

@media(max-width: 767.98px) {
  .gallery-default .img-wrapper {
    padding-bottom:60.854%;
  }
}

.gallery-default .img-wrapper.need-mask:after {
  content: "";
  position: absolute;
  width: 100%;
  height: 100%;
  left: 0;
  top: 0;
  background-color: #1a1c20;
  opacity: .4;
  z-index: 1;
}

.gallery-default .img-wrapper__content {
  position: absolute;
  width: 100%;
  height: 100%;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);
  color: #fff;
  z-index: 2;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.gallery-default .img-wrapper__content__title {
  font-weight: 800;
  font-size: 52px;
  line-height: 1;
  letter-spacing: .02em;
}

@media(max-width: 767.98px) {
  .gallery-default .img-wrapper__content__title {
    font-size:30px;
  }
}

.gallery-default .img-wrapper__content__subtitle {
  margin-top: 8px;
  line-height: 1.1;
  letter-spacing: .05em;
  padding-left: 8px;
  padding-right: 8px;
  font-weight: 600;
}

@media(max-width: 767.98px) {
  .gallery-default .img-wrapper__content__subtitle {
    margin-top:0;
    font-size: 14px;
    letter-spacing: .02em;
    line-height: 1.1;
    font-weight: 600;
    padding-left: 4px;
    padding-right: 4px;
  }
}
</style>